<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of fichiers
 *
 * @author QUYET
 */
class Fichiers extends Admin_Controller {

    function Fichiers() {
        parent::Admin_Controller();
        $this->load->model('Fichiers_m');
    }

    function index() {
        $this->liste_des_fichiers();
    }

    function liste_des_fichiers() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/fichiers/liste_des_fichiers/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '10';
        $config['total_rows'] = $this->db->count_all('pro_files');
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }

        $this->_data['fichiers'] = $this->Fichiers_m->get_all_fitchiers($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste pro offer";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['total'] = $this->Fichiers_m->count_pro_offer();
        $this->_data['page_title'] = "Liste de fichiers";
        $this->display_view('fichiers/liste_des_fichiers');
    }

    function delete_liste_des_fichiers($id, $per_page, $off_set) {
        $query = $this->Fichiers_m->get_file_name($id);
        if ($query->num_rows() > 0) {
            $file_name = $query->row();
            if ($file_name->fichier != null) {
                $path = './assets/pdf_files/' . $file_name->fichier;
                delete_pro_file($path);
            }
        }
        $this->Fichiers_m->delete_file($id);
        $this->_data['page_title'] = "Liste de fichiers";
        redirect(site_url('fichiers/liste_des_fichiers/' . $per_page . '/' . $off_set));
    }

    function liste_pro_offer() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/fichiers/liste_pro_offer/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '10';
        $config['total_rows'] = $this->Fichiers_m->count_pro_offer();
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }

        $this->_data['pro'] = $this->Fichiers_m->list_pro_offer($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste pro offer";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['total'] = $this->Fichiers_m->count_pro_offer();
        $this->_data['page_title'] = "Liste pro offer";
        $this->display_view('fichiers/liste_pro_offer');
    }

    function liste_pro_dispo() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/fichiers/liste_pro_dispo/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '10';
        $config['total_rows'] = $this->Fichiers_m->count_pro_dispo();
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }

        $this->_data['pro'] = $this->Fichiers_m->list_pro_dispo($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste pro dispo";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['total'] = $this->Fichiers_m->count_pro_dispo();
        $this->_data['page_title'] = "Liste pro dispo";
        $this->display_view('fichiers/liste_pro_dispo');
    }

    function add_pro_offer() {
        is_admin();
        $abc = array(
            array(
                'field' => 'txtName',
                'label' => 'Document Name',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($abc);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $config['upload_path'] = './assets/pdf_files';
                $config['allowed_types'] = 'pdf';
                $config['max_size'] = '100000';
                $this->load->library('upload', $config);
                $this->_data['page_title'] = "Uploading...";
                if (!$this->upload->do_upload()) {
                    $this->_data['error'] = $this->upload->display_errors();
                    $this->_data['page_title'] = "Add pro offer";
                    $this->display_view('fichiers/add_pro_offer');
                } else {
                    $upload_info = $this->upload->data();
                    $data = array(
                        'nom' => $this->input->post('txtName'),
                        'fichier' => $upload_info['file_name'],
                    );
                    $check = $this->Fichiers_m->add_pro_files($data);
                    $newID = $this->Fichiers_m->get_new_pro_files_id();
                    $file_id = $newID->row();
                    $data1 = array(
                        'file_id' => $file_id->newID,
                        'pro_id' => $this->input->post('pro')
                    );
                    //print_r($data1);die;
                    $this->Fichiers_m->add_pro_offer($data1);
                    $this->_data['page_title'] = "Liste pro offer";
                    redirect(site_url('fichiers/liste_pro_offer'));
                }
            } else {
                $this->_data['page_title'] = "Add pro offer";
                $this->display_view('fichiers/add_pro_offer');
            }
        } else {
            $this->_data['page_title'] = "Add pro offer";
            $this->display_view('fichiers/add_pro_offer');
        }
    }

    function add_pro_dispo() {
        is_admin();
        $this->_data['page_title'] = "Liste pro dispo";
        $abc = array(
            array(
                'field' => 'txtName',
                'label' => 'Document Name',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($abc);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $config['upload_path'] = './assets/pdf_files';
                $config['allowed_types'] = 'pdf';
                $config['max_size'] = '100000';

                $this->load->library('upload', $config);
                $this->_data['page_title'] = "Uploading...";
                if (!$this->upload->do_upload()) {
                    $this->_data['error'] = $this->upload->display_errors();
                    $this->_data['page_title'] = "Add pro dispo";
                    $this->display_view('fichiers/add_pro_dispo');
                } else {
                    $upload_info = $this->upload->data();
                    $data = array(
                        'nom' => $this->input->post('txtName'),
                        'fichier' => $upload_info['file_name'],
                    );
                    $this->Fichiers_m->add_pro_files($data);
                    $newID = $this->Fichiers_m->get_new_pro_files_id();
                    $file_id = $newID->row();

                    $data1 = array(
                        'file_id' => $file_id->newID,
                        'pro_id' => $this->input->post('pro')
                    );

                    $this->Fichiers_m->add_pro_dispo($data1);
                    redirect(site_url('fichiers/liste_pro_dispo'));
                }
            } else {
                $this->display_view('fichiers/add_pro_dispo');
            }
        } else {
            $this->display_view('fichiers/add_pro_dispo');
        }
    }

    function delete_pro_offer($id, $per_page, $off_set) {
        is_admin();
        $this->Fichiers_m->delete_pro_offer($id);
        redirect(site_url('fichiers/liste_pro_offer/' . $per_page . '/' . $off_set));
    }

    function delete_pro_dispo($id, $per_page, $off_set) {
        is_admin();
        $this->Fichiers_m->delete_pro_dispo($id);
        redirect(site_url('fichiers/liste_pro_dispo/' . $per_page . '/' . $off_set));
    }

    function update_pro_offer($id, $per_page, $off_set) {
        is_admin();
        $this->_data['page_title'] = "update pro offer";
        $this->_data['query'] = $this->Fichiers_m->get_pro_offer_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            $data = array(
                'pro_id' => $this->input->post('pro')
            );
            $this->Fichiers_m->update_pro_offer($data, $id);
            redirect(site_url('fichiers/liste_pro_offer/' . $per_page . '/' . $off_set));
        } else {
            $this->display_view('fichiers/update_pro_offer');
        }
    }

    function update_pro_dispo($id, $per_page, $off_set) {
        is_admin();
        $this->_data['page_title'] = "Update pro dispo";
        $this->_data['query'] = $this->Fichiers_m->get_pro_dispo_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            $data = array(
                'pro_id' => $this->input->post('pro')
            );
            $this->Fichiers_m->update_pro_dispo($data, $id);
            redirect(site_url('fichiers/liste_pro_dispo/' . $per_page . '/' . $off_set));
        } else {
            $this->display_view('fichiers/update_pro_dispo');
        }
    }

    function list_all_plv_pro() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/fichiers/list_all_plv_pro/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '10';
        $config['total_rows'] = $this->Fichiers_m->count_all_pro_plv();
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }

        $this->_data['plv_pro'] = $this->Fichiers_m->get_all_plv_pro($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste plv_pro";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['page_title'] = "Liste pro plv";
        $this->display_view('fichiers/liste_pro_plv');
    }

    function liste_pro_plv_cat() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/fichiers/liste_pro_plv_cat/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '10';
        $config['total_rows'] = $this->db->count_all('plv_pro_cat');
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }

        $this->_data['pro_plv_cat'] = $this->Fichiers_m->get_all_plv_pro_cat($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste pro plv cat";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['page_title'] = "Liste pro plv cat";
        $this->display_view('fichiers/liste_pro_plv_cat');
    }

    function delete_pro_plv($id, $per_page, $off_set) {
        $this->Fichiers_m->delete_pro_plv($id);
        redirect(site_url('fichiers/list_all_plv_pro/' . $per_page . '/' . $off_set));
    }

    function delete_pro_plv_cat($id, $per_page, $off_set) {
        $this->Fichiers_m->delete_pro_plv_cat($id);
        redirect(site_url('fichiers/liste_pro_plv_cat/' . $per_page . '/' . $off_set));
    }

    function add_pro_plv() {
        is_admin();
        $this->_data['page_title'] = "Liste pro plv";
        $abc = array(
            array(
                'field' => 'txtName',
                'label' => 'Document Name',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($abc);
        if ($this->input->post('submit')) {
            $this->_data['selected_plv_cat'] = $this->input->post('plv_cat');
            $this->_data['selected_pro'] = $this->input->post('pro');
            if ($this->form_validation->run()) {
                $config['upload_path'] = './assets/pdf_files';
                $config['allowed_types'] = 'pdf';
                $config['max_size'] = '100000';
                $this->load->library('upload', $config);
                $this->_data['page_title'] = "Uploading...";
                if (!$this->upload->do_upload()) {
                    $this->_data['error'] = $this->upload->display_errors();
                    $this->display_view('fichiers/add_pro_plv');
                } else {
                    $upload_info = $this->upload->data();
                    $data = array(
                        'nom' => $this->input->post('txtName'),
                        'fichier' => $upload_info['file_name'],
                    );
                    $check = $this->Fichiers_m->add_pro_files($data);
                    $newID = $this->Fichiers_m->get_new_pro_files_id();
                    $file_id = $newID->row();
                    $data1 = array(
                        'file_id' => $file_id->newID,
                        'plv_cat' => $this->input->post('plv_cat'),
                        'pro_id' => $this->input->post('pro')
                    );
                    $this->Fichiers_m->add_pro_plv($data1);
                    redirect(site_url('fichiers/list_all_plv_pro'));
                }
            } else {
                $this->display_view('fichiers/add_pro_plv');
            }
        } else {
            $this->display_view('fichiers/add_pro_plv');
        }
    }

    function add_pro_plv_cat() {
        is_admin();
        $this->_data['page_title'] = "Liste pro plv";
        $abc = array(
            array(
                'field' => 'nom',
                'label' => 'Nom',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($abc);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'nom' => $this->input->post('nom')
                );
                $this->Fichiers_m->add_pro_plv_cat($data);
                redirect(site_url('fichiers/liste_pro_plv_cat'));
            } else {
                $this->display_view('fichiers/add_pro_plv_cat');
            }
        } else {
            $this->display_view('fichiers/add_pro_plv_cat');
        }
    }

    function update_pro_plv($id, $per_page, $off_set) {
        is_admin();
        $this->_data['page_title'] = "Upload pro plv";
        $this->_data['query'] = $this->Fichiers_m->get_pro_plv_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            $data = array(
                'plv_cat' => $this->input->post('plv_cat'),
                'pro_id' => $this->input->post('pro')
            );
            $this->Fichiers_m->update_pro_plv($id, $data);
            redirect(site_url('fichiers/list_all_plv_pro/' . $per_page . '/' . $off_set));
        } else {
            $this->display_view('fichiers/update_pro_plv');
        }
    }

    function update_pro_plv_cat($id, $per_page, $off_set) {
        is_admin();
        $abc = array(
            array(
                'field' => 'nom',
                'label' => 'nom',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($abc);
        $this->_data['page_title'] = "Upload pro plv cat";
        $this->_data['query'] = $this->Fichiers_m->get_pro_plv_cat_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'nom' => $this->input->post('nom')
                );
                $this->Fichiers_m->update_pro_plv($id, $data);
                redirect(site_url('fichiers/liste_pro_plv_cat/' . $per_page . '/' . $off_set));
            } else {
                $this->display_view('fichiers/update_pro_plv_cat');
            }
        } else {
            $this->display_view('fichiers/update_pro_plv_cat');
        }
    }

}

?>